﻿using System;
using System.Web.UI;
using Gasco.DataAccess;
using Gasco.Business.Services;


public partial class Controls_company_contact_add : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            if (!Page.IsPostBack)
            {
                //Load Contact Info
                ContactService cs = new ContactService();
                contact c = cs.GetContactById(Convert.ToInt16(Request.QueryString["id"]));
                tbName.Text = c.name;
                tbRegisterCode.Text = c.company_contact.register_code;
                tbCEOName.Text = c.company_contact.ceo_name;
                tbCEOTel.Text = c.company_contact.ceo_tel;
                tbCEOMobile.Text = c.company_contact.ceo_mobile;
                tbCORRName.Text = c.company_contact.corr_name;
                tbCORRTel.Text = c.company_contact.corr_tel;
                tbCORRMobile.Text = c.company_contact.corr_mobile;
                tbFax.Text = c.fax;
                tbEmail.Text = c.email;
                tbAccountInfo.Text = c.account_info;
                tbAddress.Text = c.address;
                tbActivityField.Text = c.activity_field;
            }
        }
    }
    protected void btnclose_Click(object sender, EventArgs e)
    {
     InsertUpdateInfo();
     Page.ClientScript.RegisterClientScriptBlock(typeof(Page),"Script1","<script>pageClose();</script>");
    }

    

    private void InsertUpdateInfo()
    {

        bool InsertMode = (Request.QueryString["id"] == null);
        ContactService cs = new ContactService();
        contact c;

        if(InsertMode)
        {
            c = new contact();
            c.company_contact=new company_contact();
        }
        else
        {
            c = cs.GetContactById(Convert.ToInt32(Request.QueryString["id"]));
        }
        
        //Contact Info
        c.name = tbName.Text;
        c.account_info = tbAccountInfo.Text;
        c.email = tbEmail.Text;
        c.fax = tbFax.Text;
        c.contact_type_id = 2;//شخصيت حقوقي
        c.address = tbAddress.Text;
        c.activity_field = tbActivityField.Text;
        c.update_date = DateTime.Now;

        //Company Contact Info
        c.company_contact.ceo_name = tbCEOName.Text;
        c.company_contact.ceo_tel = tbCEOTel.Text;
        c.company_contact.ceo_mobile = tbCORRMobile.Text;
        c.company_contact.register_code = tbRegisterCode.Text;
        c.company_contact.corr_name = tbCORRName.Text;
        c.company_contact.corr_tel = tbCORRTel.Text;
        c.company_contact.corr_mobile = tbCORRMobile.Text;
        c.company_contact.update_date = DateTime.Now;

         if(InsertMode){
            c.create_date = DateTime.Now;
            c.company_contact.create_date = DateTime.Now;
            cs.CreateContact(c);
        }
        else
        {
            cs.UpdateContact(c);
        }
    }
}
